Unit mnCommThreads;
{**
 *  This file is part of the "Mini Comm"
 *
 * @license   modifiedLGPL (modified of http://www.gnu.org/licenses/lgpl.html)
 *            See the file COPYING.MLGPL, included in this distribution,
 * @author    Zaher Dirkey <zaher at parmaja dot com>
 *}

  {$M+}
  {$H+}
  {$IFDEF FPC}
  {$MODE delphi}
  {$ENDIF}

Interface

Uses
    Classes, SysUtils,
    mnCommClasses;

Type

    { TmnCommThread }

    TmnCommThread = Class(TThread)
    private
        FCommStream: TmnCustomCommStream;
        FUseWaitEvent: Boolean;
    protected
        Procedure DoTerminate; override;
        Procedure StringArrived(S: String); virtual;
        Procedure Execute; override;
    public
        Constructor Create(CreateSuspended: Boolean;
            CommStream: TmnCustomCommStream);
        Destructor Destroy; override;
        Property UseWaitEvent: Boolean read FUseWaitEvent write FUseWaitEvent;
        Property CommStream: TmnCustomCommStream read FCommStream write FCommStream;
    End;

    TmnCommStreamThread = Class(TmnCommThread)
    private
        Procedure InternalStringArrived; 
    protected
        FBuffer: String;
        Procedure StringArrived(S: String); override;
        Procedure DoStringArrived(S: String); virtual;
    public
        Constructor Create(CreateSuspended: Boolean;
            CommStream: TmnCustomCommStream);
    End;

  
Implementation


{ TmnCommThread }

Constructor TmnCommThread.Create(CreateSuspended: Boolean;
    CommStream: TmnCustomCommStream);
Begin
    Inherited Create(CreateSuspended);
    FCommStream := CommStream;
End;

Destructor TmnCommThread.Destroy;
Begin
    If FreeOnTerminate Then FreeAndNil(FCommStream);
    Inherited Destroy;
End;

Procedure TmnCommThread.DoTerminate;
Begin
    If FCommStream <> Nil Then
    Begin
        FCommStream.Cancel;
        If FreeOnTerminate Then FCommStream.Close;
    End;
    Inherited;
End;

Procedure TmnCommThread.Execute;
Begin
    Try
        // You muse have EventChar and QueMode
        While not Terminated and FCommStream.Connected Do
        Begin
            If not UseWaitEvent or (FCommStream.WaitEvent([evError, evRxFlag]) <> [])
                Then;
            Begin
                If not Terminated and FCommStream.Connected Then
                Begin
                    StringArrived(FCommStream.ReadString);
                End;
            End;
        End;
    Finally
    End;
End;

Procedure TmnCommThread.StringArrived(S: String);
Begin
End;

{ TmnCommStreamThread }

Constructor TmnCommStreamThread.Create(CreateSuspended: Boolean;
    CommStream: TmnCustomCommStream);
Begin
    Inherited Create(CreateSuspended, CommStream);
    If CommStream.ReadTimeout = 0 Then //We will use QueMode
    CommStream.QueMode := True;
End;

Procedure TmnCommStreamThread.DoStringArrived(S: String);
Begin
End;

Procedure TmnCommStreamThread.InternalStringArrived;
Begin
    DoStringArrived(FBuffer);
End;

Procedure TmnCommStreamThread.StringArrived(S: String);
Begin
    FBuffer := S;
    InternalStringArrived;
End;

End.
